package com.syt.hospital.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syt.exception.SYTException;
import com.syt.hospital.mapper.HospitalSetMapper;
import com.syt.hospital.repository.HospitalRepository;
import com.syt.hospital.service.HospitalSetService;
import com.syt.model.hospital.Hospital;
import com.syt.model.hospital.HospitalSet;
import com.syt.result.ResultCodeEnum;
import com.syt.utils.MD5;
import com.syt.vo.order.SignInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Random;

/**
 * <p>
 * 医院设置表 服务实现类
 * </p>
 *
 * @author xxx
 * @since 2023-02-01
 */
@Service
public class HospitalSetServiceImpl extends ServiceImpl<HospitalSetMapper, HospitalSet> implements HospitalSetService {

   @Autowired
   private HospitalRepository hospitalRepository;

    /**
     * 获取医院签名信息
     * @param hoscode
     * @return
     */
    @Override
    public SignInfoVo getSignInfoVo(String hoscode) {
        QueryWrapper<HospitalSet> wrapper=new QueryWrapper<>();
        wrapper.eq("hoscode", hoscode);
        HospitalSet hospitalSet = baseMapper.selectOne(wrapper);
        if (hospitalSet==null) {
            throw new SYTException(ResultCodeEnum.HOSPITAL_OPEN);
        }
        SignInfoVo signInfoVo = new SignInfoVo();
        signInfoVo.setApiUrl(hospitalSet.getApiUrl());
        signInfoVo.setSignKey(hospitalSet.getSignKey());
        return signInfoVo;
    }

    /**
     * 根据医院编号获取预约规则
     * @param hoscode
     * @return
     */
    @Override
    public Hospital getByHoscode(String hoscode) {
        return hospitalRepository.getHospitalByHoscode(hoscode);
    }

    /**
     * 获取医院名称
     * @param hoscode
     * @return
     */
    @Override
    public String getHospName(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        if (hospital!=null) {
            return hospital.getHosname();
        }
        return null;
    }

    /**
     * 根据传递过来医院编码，查询数据库，查询签名
     * @param hoscode
     * @return
     */
    @Override
    public String getSinKey(String hoscode) {

        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        wrapper.eq("hoscode",  hoscode);
        HospitalSet hospitalSet = baseMapper.selectOne(wrapper);
        if (hospitalSet==null){
            throw new SYTException("已删除", 201);
        }
        return hospitalSet.getSignKey();
    }

    /**
     * 修改医院设置锁定或解锁
     * @param id
     * @param status
     * @return
     */
    @Transactional
    @Override
    public boolean lockHospitalSet(long id, int status) {
        //1、根据id查询医院信息
        HospitalSet hospitalSet = baseMapper.selectById(id);
        //2、设置医院状态
        hospitalSet.setStatus(status);
        //3、更新医院设置信息
        int i = baseMapper.updateById(hospitalSet);
        if (i==1){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 批量删除医院设置
     * @param idList
     * @return
     */
    @Override
    public boolean batchRemoveHospitalSet(List<Long> idList) {
        int i = baseMapper.deleteBatchIds(idList);
        if (i>0){
            return true;
        }else {
            return false;
        }
        
    }

    /**
     * 根据id修改医院设置
     *
     * @param hospitalSet
     * @return
     */
    @Transactional
    @Override
    public boolean updateHospitalSetById(HospitalSet hospitalSet) {
        int update = baseMapper.updateById(hospitalSet);
        if (update == 1) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 根据医院Id进行逻辑删除
     *
     * @param id
     * @return
     */
    @Transactional
    @Override
    public boolean removeHospitalById(Long id) {
        int i = baseMapper.deleteById(id);
        if (i == 1) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 添加医院设置
     *
     * @param hospitalSet
     * @return
     */
    @Transactional
    @Override
    public boolean saveHospitalSet(HospitalSet hospitalSet) {
        
        //1、设置状态 1使用、0不适用
        hospitalSet.setStatus(1);
        //2、签名密钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));
        //3、添加数据库中
        int insert = baseMapper.insert(hospitalSet);

        if (insert == 1) {
            return true;
        } else {
           return false;
        }

    }
}
